import {createRouter,createWebHashHistory} from  'vue-router'

// 导入pinia数据
import pinia from '../pinia.js'
import {defineUser} from '../store/userStore.js'
let user=defineUser(pinia)


import ShowSchedule from '../components/ShowSchecule.vue'
import Login from '../components/Login.vue'
import Regist from '../components/Regist.vue'
let router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            redirect:"/showSchedule"
        },
        {
            path:"/showSchedule",
            component:ShowSchedule
        },
        {
            path:"/login",
            component:Login
        },
        {
            path:"/regist",
            component:Regist
        }
    ]
})

// 设置全局前置守卫,控制未登录时 不让进入showSchedule
router.beforeEach(function(to,from,next){
    // 如果登陆过 放行
    // 根据pinia数据状态判断
    if(user.username != ''){
        next()
    }else{
        // 如果未登录 请求的路径是/showSchedule则重定向到/login
        //           跳转的是/login /regist
        if(to.path == '/showSchedule'){
            next("/login")
        }else{
            next()
        }
    }




})

export default router